public class MyQueue {
    static class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;
    public Node last;
    public int usedSize;

    //入队列
    public void offer(int data){
        Node node = new Node(data);
        //队列为空时:
        if(head == null){
            head = node;
            last = node;
        }else{
            //队列不为空时，单向链表的尾部入队列：
            last.next = node;
            last = node;
        }
        usedSize++;
    }

    //出队列
    public int poll(){
        //队列为空时：
        if(empty()){
            throw new EmptyQueueException("队列为空，出队列失败！");
        }
        int val = head.val;
        //队列只有一个元素时：
        if(head.next == null){
            last = last.next;
        }
        head = head.next;
        usedSize--;
        return val;
    }

    public int peek(){
        if(empty()){
            throw new EmptyQueueException("队列为空，队头的首个元素获取失败！");
        }
        return head.val;
    }

    public boolean empty(){
        return usedSize == 0;
    }

    public int getUsedSize(){
        return usedSize;
    }
}
